package com.ming.rabbitmq.eight;

import com.ming.rabbitmq.util.RabbitMqUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.util.HashMap;

public class Consumer01 {

    public static final String NORMAL_EXCHANGE="normal_exchange2";
    public static final String DEAD_EXCHANGE="dead_exchange2";
    public static final String DEAD_QUEUE="dead_queue2";
    public static final String NORMAL_QUEUE="normal_queue2";

    public static void main(String[] args) throws Exception {

        Channel channel = RabbitMqUtils.getChannel();
        channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
        channel.exchangeDeclare(DEAD_EXCHANGE,BuiltinExchangeType.DIRECT);

        channel.queueDeclare(DEAD_QUEUE,false,false,false,null);
        channel.queueBind(DEAD_QUEUE,DEAD_EXCHANGE,"lisi");

        HashMap<String, Object> params = new HashMap<>();
        params.put("x-dead-letter-exchange",DEAD_EXCHANGE);
        params.put("x-dead-letter-routing-key","lisi");
        //params.put("x-max-length",6);
        channel.queueDeclare(NORMAL_QUEUE,false,false,false,params);
        channel.queueBind(NORMAL_QUEUE,NORMAL_EXCHANGE,"zhangsan");

        DeliverCallback deliverCallback = (consumerTag, message) -> {
            if (new String(message.getBody()).equals("info: 5")){
                System.out.println("Consumer01接收到的消息："+new String(message.getBody())+" ,此消息被拒绝了");
                channel.basicReject(message.getEnvelope().getDeliveryTag(),false);
            }
            channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
            System.out.println("Consumer01接收到的消息："+new String(message.getBody()));
//            System.out.println(message.getEnvelope().getRoutingKey());
        };
        channel.basicConsume(NORMAL_QUEUE, false, deliverCallback, consumeTag->{});

    }

}
